﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;

namespace SuperStore.Models
{
    public static class Orders
    {
        private static Guid GetOrderID(string memberID)
        {
            Guid memberGUID = Guid.Parse(memberID);
            try
            {
                Guid existingOrder = (from o in Database.Instance.Orders
                                      where o.State == OrderState.InProgress.ToString() &&
                                      o.MemberID == memberGUID
                                      select o.PK).Single();

                return existingOrder;
            }
            catch
            {
                Order newOrder = new Order()
                {
                    MemberID = memberGUID,
                    OrderID = Database.Instance.Orders.Count(),
                    PK = Guid.NewGuid(),
                    State = OrderState.InProgress.ToString()
                };

                Database.Instance.InsertOrder(newOrder);

                return newOrder.PK;
            }
        }

        public static List<CartItem> GetCart(string memberID)
        {
            Guid existingOrder = GetOrderID(memberID);

            return (from o in Database.Instance.OrderItems
                    join g in Database.Instance.Games on o.GameID equals g.PK
                    where o.OrderID.Equals(existingOrder)
                    select new CartItem() { OrderID = existingOrder, Quantity = o.Amount, MSPoints = o.MSPointsPerItem, RetailCost = o.CostPerItem, Arcade = g.Arcade, Title = g.Title, OrderItemID = o.PK }).ToList();
        }

        public static void Add(string memberID, string gameID)
        {
            Guid orderID = GetOrderID(memberID);
            Guid gameGUID = Guid.Parse(gameID);

            Game game = (from g in Database.Instance.Games
                        where g.PK.Equals(gameGUID)
                        select g).Single();

            OrderItem orderItem = new OrderItem()
            {
                Amount = 1,
                CostPerItem = game.RetailCost,
                GameID = gameGUID,
                MSPointsPerItem = game.MSPointsCost,
                OrderID = orderID,
                PK = Guid.NewGuid()
            };

            Database.Instance.InsertOrderItems(orderItem);
        }

        public static void Remove(Guid orderItemID)
        {
            OrderItem orderItem = (from o in Database.Instance.OrderItems
                                  where o.PK.Equals(orderItemID)
                                  select o).Single();

            Database.Delete(orderItem);
        } 


        public static void CheckOut(string memberID)
        {
            Guid cartID = GetOrderID(memberID);

            Order order = (from o in Database.Instance.Orders
                           where o.PK.Equals(cartID)
                           select o).Single();

            order.State = OrderState.Done.ToString();

            Database.Update(order);
        }

        public static void ChangeQuantity(Guid orderItemID, long quantity)
        {
            if (quantity <= 0)
            {
                Remove(orderItemID);
                return;
            }

            OrderItem item = (from o in Database.Instance.OrderItems
                              where o.PK.Equals(orderItemID)
                              select o).Single();

            if (item.Amount != quantity)
            {
                item.Amount = quantity;

                Database.Update(item);
            }
        }
    }
}